//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing.Diagram
{
    internal class LayoutVariablePropertySet : IOfficeXmlSerializableAs, IOfficeXmlSerializable
    {
        private bool _orgChart;
        private int _chMax;
        private int _chPref;
        private bool _bulletEnabled;
        private Direction? _dir;
        private HierarchyBranchStyle? _hierBranch;
        private OneByOneAnimation? _animOne;
        private AnimationLevel? _animLvl;
        private ResizeHandles? _resizeHandles;


        public void ReadXmlAs(string name , ReadContext context)
        {
            using (var c = context.Read(name))
            {
                _orgChart = c.ReadOptionalElementBooleanAttribute("orgChart", false);
                _chMax = c.ReadOptionalElementIntegerAttribute("chMax", -1);
                _chPref = c.ReadOptionalElementIntegerAttribute("chPref", -1);
                _bulletEnabled = c.ReadOptionalElementBooleanAttribute("bulletEnabled", false);
                _dir = ReadOptionalEnum(c, "dir", Direction.Normal);
                _hierBranch = ReadOptionalEnum(c, "hierBranch", HierarchyBranchStyle.Standard);
                _animOne = ReadOptionalEnum(c, "animOne", OneByOneAnimation.OneByOne);
                _animLvl = ReadOptionalEnum(c, "animLvl", AnimationLevel.None);
                _resizeHandles = ReadOptionalEnum(c, "resizeHandles", ResizeHandles.Relative);
            }

        }
        T? ReadOptionalEnum<T>(ReadContext context, string name, T defaultValue) where T : struct
        {
            if (context.ReaderLocalName != name)
                return null;
            using (var enumC = context.Read(name))
            {
                var result = enumC.GetOptionalEnum<T>("val", defaultValue);
                return result;
            }
        }

        public void WriteXmlAs( string name, WriteContext context)
        {
            using (var c = context.Write(name))
            {
                c.WriteOptionalElementBooleanAttribute("orgChart", _orgChart, false);
                c.WriteOptionalElementIntegerAttribute("chMax", _chMax, -1);
                c.WriteOptionalElementIntegerAttribute("chPref", _chPref, -1);
                c.WriteOptionalElementBooleanAttribute("bulletEnabled", _bulletEnabled, false);
                WriteOptionalEnum(c, "dir", _dir, Direction.Normal);
                WriteOptionalEnum(c, "hierBranch", _hierBranch, HierarchyBranchStyle.Standard);
                WriteOptionalEnum(c, "animOne", _animOne, OneByOneAnimation.OneByOne);
                WriteOptionalEnum(c, "animLvl", _animLvl, AnimationLevel.None);
                WriteOptionalEnum(c, "resizeHandles", _resizeHandles, ResizeHandles.Relative);
            }
        }
        static void WriteOptionalEnum<T> (WriteContext c, string name, T? value, T defaultValue) where T : struct
        {
            if (value == null)
                return;
            using (var c2 = c.Write(name))
            {
                c2.SetOptionalEnum("val", value.Value, defaultValue);
            }

        }

        /// <summary>
        /// Generates an object from its XML representation
        /// </summary>
        /// <param name="context"></param>
        public void ReadXml(ReadContext context)
        {
            ReadXmlAs("varLst", context);
        }

        /// <summary>
        /// Convert an object into its XML representation
        /// </summary>
        /// <param name="context"></param>
        public void WriteXml(WriteContext context)
        {
            WriteXmlAs("varLst", context);
        }
    }
}